//
//  MHTopicMoreBookShowCollectionViewController.m
//  ManHua
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHTopicMoreBookShowCollectionViewController.h"
#import <AFNetworking.h>
#import "MHTopicBannerModel.h"
#import "MHShowMoreBookCollectionViewCell.h"
#import "MHTopicMoreBookListCollectionViewController.h"
@interface MHTopicMoreBookShowCollectionViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@property(nonatomic,strong)UICollectionView *collectionView;
@end

@implementation MHTopicMoreBookShowCollectionViewController


-(NSMutableArray *)dataSourceArr
{if (_dataSourceArr==nil )
{
   self.dataSourceArr=[NSMutableArray arrayWithCapacity:0];
}
    return _dataSourceArr;
}
-(void)acquireData
{
    NSMutableDictionary *parDict=[NSMutableDictionary dictionary];
    parDict[@"method"]=@"GetTopicLikeByTopicID";
    
    parDict[@"TopicID"]=self.mainTopicModel.MoreIDs;
    [[AFHTTPRequestOperationManager manager] GET:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx" parameters:parDict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
       
      [responseObject enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          
          MHTopicBannerModel *bannerModel=[[MHTopicBannerModel alloc]init];
          [bannerModel setValuesForKeysWithDictionary:obj];
          [self.dataSourceArr addObject:bannerModel];
      }];
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
      
        NSLog(@"+++数据解析失败原因+++：%@",error.localizedDescription);
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
      [self acquireData];
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Width, Height) collectionViewLayout:flowLayout];
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.collectionView];
  
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    
       
    [self.collectionView registerNib:[UINib nibWithNibName:@"MHShowMoreBookCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"showBookCVC"];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   MHShowMoreBookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"showBookCVC"forIndexPath:indexPath];
    cell.bannerModel=self.dataSourceArr[indexPath.row];
    // Configure the cell
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    CGFloat pan=10;
    NSInteger row=2;
    CGFloat ItemW=(Width-2*pan-(row-1)*pan)/row;
    CGFloat ItemY=(ItemW)*(4.0/7.0)+25;
    return CGSizeMake(ItemW, ItemY);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
{
    return 10;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    MHTopicMoreBookListCollectionViewController *moreBookListCVC=[[MHTopicMoreBookListCollectionViewController alloc]init];
    moreBookListCVC.bannerModel=self.dataSourceArr[indexPath.row];
    moreBookListCVC.title=moreBookListCVC.bannerModel.TopicName;
    [self.navigationController pushViewController:moreBookListCVC animated:YES];
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
